package com.ymatou.iapi.sellertrading.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.BatchDelayAutoReceiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtOrderFrozen;
import com.ymttest.database.sqlwapper.YmtOrderFrozenWapper;
/**
*批量延迟收货
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BatchDelayAutoReceive{
	private static BatchDelayAutoReceiveCall batchdelayautoreceiveCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量延迟收货");
	}

	@Before
	public void caseUp() {

		batchdelayautoreceiveCall=new BatchDelayAutoReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchdelayautoreceiveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_BatchDelayAutoReceive_001() {
		Logger.start(true,"批量延迟收货成功");
		try {
			//TODO 补全case代码
			int delaydays=2;
			YmtOrderFrozenWapper ymtOrderFrozenWapper=new YmtOrderFrozenWapper();
			
			//
			List<YmtOrderFrozen> orderlist=ymtOrderFrozenWapper.selectByTradingStatus(3);
			
			//获取OrderId
			List<Integer> orders = new ArrayList<>();
			orderlist.forEach(O->orders.add(O.getiOrderId()));			
			int orderNum=orders.size();


			//定义一个Map,保存orderId和FrozenTime
			Map<Integer, Date> map1 = new HashMap<Integer, Date>();
			//根据orderId遍历
			for(int i=0;i<orderNum;i++){
				map1.put(orderlist.get(i).getiOrderId(), orderlist.get(i).getdFrozenTime());
			}
			
			//组装参数
			BatchDelayAutoReceiveCall batchDelayAutoReceiveCall=new BatchDelayAutoReceiveCall(3383,3383);			
		    Map<String, Object> updateMap = new HashMap<String,Object>();
		    updateMap.put("DelayDays", delaydays);
		    updateMap.put("OrderIds", new JSONArray(orders));	
		    
		    //填充参数，并获取JSONObject类型的返回数据
		    JSONObject batchdelayautoreceive=batchDelayAutoReceiveCall.callAndGetReturnData(updateMap);	
		    
		    //断言
		    int code=batchdelayautoreceive.getInt("Code");
		    Logger.verifyEquals(200, code, "报文返回Code正确");
		    String msg=batchdelayautoreceive.getString("Msg");
		    Logger.verifyIsContains("操作成功", msg, "操作成功");	
		    JSONObject responseData=batchdelayautoreceive.getJSONObject("Data");
		    int noDelayNum=Integer.parseInt(responseData.get("NoDelayNum").toString());
		    int delayNum =Integer.parseInt(responseData.get("DelayNum").toString());
		    Logger.verifyEquals(0, noDelayNum, "验证自动延期订单失败数为0");
            Logger.verifyEquals(orderNum, delayNum, "验证自动延期订单数相等");
            
            //
			Map<Integer, Date> map2 = new HashMap<Integer, Date>();			
			for(int i=0;i<orderNum;i++){
				map2.put(orderlist.get(i).getiOrderId(), orderlist.get(i).getdFrozenTime());
			}
			
			//比较前后的FrozenTime
			for (Integer orderId:map1.keySet()){				
				Date beforeDate = map1.get(orderId);
				Logger.debug(beforeDate);
				Date afterDate = map2.get(orderId);	
				Logger.debug(afterDate);
			    long calFozenTime=(afterDate.getTime()-beforeDate.getTime())/1000;
			    long calday=calFozenTime/(24*3600);
			    Logger.debug(calday);
			    long dday= new Long((long)delaydays);
			    
			    //验证相差的天数=delaydays
			    Logger.verifyEquals(dday, calday, "订单"+orderId+"的Delay天数正确");
				
			}
		  
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_BatchDelayAutoReceive_002() {
		Logger.start(true,"订单号不存在");
		try {
			//TODO 补全case代码
			int delaydays=2;
						
			//获取OrderId
			List<Integer> orders = new ArrayList<>();
			orders.add(123);
			orders.add(234);
			
			//组装参数
			BatchDelayAutoReceiveCall batchDelayAutoReceiveCall=new BatchDelayAutoReceiveCall(3383,33383);			
		    Map<String, Object> updateMap = new HashMap<String,Object>();
		    updateMap.put("DelayDays", delaydays);
		    updateMap.put("OrderIds", new JSONArray(orders));	
		    
		    //填充参数，并获取JSONObject类型的返回数据
		    JSONObject batchdelayautoreceive=batchDelayAutoReceiveCall.callAndGetReturnData(updateMap);	
		    
		    //断言
		    int code=batchdelayautoreceive.getInt("Code");
		    Logger.verifyEquals(200, code, "报文返回Code正确");
		    String msg=batchdelayautoreceive.getString("Msg");
		    Logger.verifyIsContains("操作成功", msg, "操作成功");	
		    JSONObject responseData=batchdelayautoreceive.getJSONObject("Data");
		    int noDelayNum=Integer.parseInt(responseData.get("NoDelayNum").toString());
		    int delayNum =Integer.parseInt(responseData.get("DelayNum").toString());
		    Logger.verifyEquals(2, noDelayNum, "验证自动延期订单失败数为2");
		    Logger.verifyEquals(0, delayNum, "验证自动延期订单成功数为0");
		    
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}